package com.example.demo.controller;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;

import com.example.demo.dao.EmpDao;
import com.example.demo.entity.Employee;

@Controller
public class TestController {
	@Autowired
    private EmpDao empdao;

	public EmpDao getEmpdao() {
		return empdao;
	}

	public void setEmpdao(EmpDao empdao) {
		this.empdao = empdao;
	}
	
	@GetMapping("/indexa")
	public String getcount(Model model) {
		int i= empdao.getempcount();
		System.out.print(i);
		model.addAttribute("empcount", i);
		return "index";
	}
	
	@GetMapping("/getemps")
	public String getemps() {
		List<Employee> list= empdao.getEmps();
		for(Employee emp:list) {
			System.out.println(emp.getEmployee_id()+"--"+emp.getFirst_name()+"--"+emp.getLast_name());
			
		}
		return "index";
	}
	
	@GetMapping("/getempbytype")
	public String getempbytype() {
		Map<String,Object> map=new HashMap<String,Object>();
		map.put("empidmap", 150);
		map.put("salarymap", 5000);
		
		List<Employee> list=empdao.getEmpByType2(map);
		for(Employee emp:list) {
			System.out.println(emp.getEmployee_id()+"--"+emp.getFirst_name()+"--"+emp.getLast_name()+"--"+emp.getSalary());			
		}
		return "index";
	}
	
	@GetMapping("/getempbydept")
	public String getempbydept() {
		List<Employee> list= empdao.getEmpBydept();
		for(Employee emp:list) {
			System.out.println(emp.getEmployee_id()+"--"+emp.getFirst_name()+"--"+emp.getDepartment_id()+"--"+emp.getDeptname());			
		}
		return "index";
	}
	
	@GetMapping("/addemp")
	public String addemp() throws ParseException  {
		Employee emp=new Employee();
		emp.setEmployee_id(208);
		emp.setFirst_name("rose");
		emp.setLast_name("rose");
		emp.setEmail("sdfhgfer");
		emp.setSalary(8000.5);
		emp.setManager_id(205);
		emp.setDepartment_id(50);
		SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd");
		Date date= sdf.parse("2000-01-01");
		emp.setHiredate(date);
		int i= empdao.addEmp(emp);
		System.out.print(i);
		
		return "index";
	}
	
	@GetMapping("/deleteemp")
	public String deleteemp() {
		int i= empdao.deleteEmp(208);
		System.out.print(i);
		return "index";
	}
	@GetMapping("/updateemp")
	public String updateemp() throws ParseException {
		Employee emp=new Employee();
		emp.setEmployee_id(206);
		emp.setFirst_name("jack");
		
		emp.setEmail("iklohgu");
		emp.setSalary(8000.5);
		emp.setManager_id(205);
		emp.setDepartment_id(50);
		SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd");
		Date date= sdf.parse("2000-01-01");
		emp.setHiredate(date);
		
		int i= empdao.updateEmp(emp);
		
		System.out.print(i);		
		return "index";
	}
	@GetMapping("/getempbyid")
	public String getempbyid() {
		Employee emp= empdao.getempbyid(206);
		System.out.print(emp.getEmployee_id());
		System.out.print(emp.getFirst_name());
		System.out.print(emp.getLast_name());
		System.out.print(emp.getSalary());
		
		return "index";
	}
	
	//1.查询employees，departments，locations 做表连接，employees==》employee_id,first_name,last_name,salary,department_id
	//departments==>department_name,location_id,
	//locations==>street_address,postal_code,city
	//把查询出来的集合展示到前端页面
	//2.查询平均工资高于5000元的，每个部门的平均工资，最高工资，最低工资，department_name，avg,max,min,把查询出来的集合展示到前端页面
	//3.查询员工表中所有经理的员工信息，把查询出来的集合展示到前端页面
}
